package com.cindata

import grails.gorm.transactions.Transactional
import org.springframework.security.access.annotation.Secured

import grails.converters.JSON
import java.text.SimpleDateFormat
import org.grails.web.util.WebUtils

import com.cindata.dictionary.ComponentType

import static org.springframework.http.HttpStatus.*

@Secured(['isAuthenticated()'])
@Transactional
class WorkflowStageConditionController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond WorkflowStageCondition.list(params), model:[workflowStageConditionCount: WorkflowStageCondition.count()]
    }

    def show(WorkflowStageCondition workflowStageCondition) {
        respond workflowStageCondition
    }

    def create() 
    {
        params['targetUri'] = request.getHeader("referer")
        def workflowStage = WorkflowStage.findById(params['workflowStage'])
        def workflowStages = WorkflowStage.findAllByWorkflow(workflowStage?.workflow)
        respond new WorkflowStageCondition(params), model: [workflowStageStages: workflowStages]
    }

    @Transactional
    def save(WorkflowStageCondition workflowStageCondition) 
    {
        if (workflowStageCondition == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (workflowStageCondition.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond workflowStageCondition.errors, view:'create'
            return
        }

        workflowStageCondition.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'workflowStageCondition.label', default: 'WorkflowStageCondition'), workflowStageCondition.id])
                redirect url: params['targetUri']
            }
            '*' { respond workflowStageCondition, [status: CREATED] }
        }
    }

    def edit(WorkflowStageCondition workflowStageCondition)
    {
        params['targetUri'] = request.getHeader("referer")
        def workflowStages = WorkflowStage.findAllByWorkflow(workflowStageCondition?.workflowStage?.workflow)
        respond workflowStageCondition, model: [workflowStageStages: workflowStages]
    }

    @Transactional
    def update(WorkflowStageCondition workflowStageCondition) {
        if (workflowStageCondition == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (workflowStageCondition.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond workflowStageCondition.errors, view:'edit'
            return
        }

        workflowStageCondition.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'workflowStageCondition.label', default: 'WorkflowStageCondition'), workflowStageCondition.id])
                redirect url: params['targetUri']
            }
            '*'{ respond workflowStageCondition, [status: OK] }
        }
    }

    @Transactional
    def delete(WorkflowStageCondition workflowStageCondition) 
    {

        def targetUri = request.getHeader("referer")

        if (workflowStageCondition == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        workflowStageCondition.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'workflowStageCondition.label', default: 'WorkflowStageCondition'), workflowStageCondition.id])
                redirect url: targetUri
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'workflowStageCondition.label', default: 'WorkflowStageCondition'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
